(function($) {
	/***************************************************************************
	 * JQuery 明细视图插件12.0
	 * 
	 * Author: 姜志光 Date: 2014年1月8日
	 **************************************************************************/

	TreeView = function(rootElement, userSettings) {

		var instance = this;

		/***********************************************************************
		 * 数据来源表单
		 **********************************************************************/
		this.fromForm = userSettings.fromForm;

		/***********************************************************************
		 * 视图主键配置
		 **********************************************************************/
		this.pfields = userSettings.pfields;

		this.viewConfig = userSettings;

		/***********************************************************************
		 * 视图当前选中行
		 **********************************************************************/
		this.selectRow;

		this.viewOperate = null;

		/***********************************************************************
		 * 视图初始化
		 **********************************************************************/
		this.init = function() {

			/*******************************************************************
			 * 树视图初始化
			 ******************************************************************/
			var url = application
					+ "/browse/view/showTreeViewDatas.action?vv_guid="
					+ instance.viewConfig.vv_guid + "&tt_name="
					+ instance.viewConfig.tt_name;
//			var url	=	application+"/tree_data1.json";
			$("#westTree").tree({
				url : url,
				lines:true,
				onClick: function(node) {
	                if (node.id == "00") {
	                } else {
	                   instance.viewOperate.refreshOperates();
	                }
	            },
			});
			instance.viewOperate = new ViewOperate(instance,
					instance.viewConfig.operateConfig, $("#operateDiv"));
			/*******************************************************************
			 * 视图操作初始化
			 ******************************************************************/
			instance.viewOperate.operateDraw();

			instance.onAdd();
			var searchFormInputs = $("#searchFormInputs");
			if (searchFormInputs.length == 0) {
				return;
			}

//			var height = $("#searchFormInputs").height();
//			$('#contentLayout').layout('panel', 'north').panel('resize', {
//				height : height + 58
//			});
//			$('#contentLayout').layout('resize');
		};

		/***********************************************************************
		 * 数据添加操作
		 **********************************************************************/
		this.onAdd = function() {
			if ('' != instance.fromForm) {
				var url	=	application + '/goForm?vf_guid='+instance.fromForm+'&inside=1';
				$("#friends").load(url);
//				$("#groupIFrame").attr("src",
//						application + '/goForm?vf_guid=' + instance.fromForm);
//				$('#cc').layout('panel', 'center').panel({title:"节点添加"});
			}
		};

		/***********************************************************************
		 * 数据编辑操作
		 **********************************************************************/
		this.onEdit = function() {
			if ($("#detailForm").length == 0) {
				alert("未配置数据来源表单");
				return false;
			}
			var node = instance.getSelectedRow();
			if (undefined == node) {
				alert("未选中节点");
				return;
			}
			
			$("#detailDiv").html("");
			for ( var x = 0; x < instance.pfields.length; x++) {
				$("#detailDiv").append(
						"<input type='hidden' name='viewPValues." + instance.pfields[x]
								+ "' value='" + node.attributes[instance.pfields[x]] + "'/>");
			}
			
			//var id	=	node.id;
			//$("#editCode").val(id);
			$('#cc').layout('panel', 'center').panel({title:"【"+node.text+"】 节点修改"});
			$("#detailForm").submit();
		};

		/***********************************************************************
		 * 数据删除操作
		 **********************************************************************/
		this.onDelete = function() {
			
			var node = $('#westTree').tree('getSelected');
			if(undefined == node){
				alert("未选中节点");
				return;
			}
			
			if(!confirm("您确定要删除该节点")){
				return;
			}
			if($("#detailForm").length == 0){
				alert("未配置数据来源表单");
				return;
			}

			$("#deleteDiv").html("");
			for ( var x = 0; x < instance.pfields.length; x++) {
				$("#deleteDiv").append(
						"<input type='hidden' name='pvalues." + instance.pfields[x]
								+ "' value='" + node.attributes[instance.pfields[x]] + "'/>");
			}
			$('#deleteForm').ajaxSubmit(function(data) {
				if ("成功删除表单数据" == data) {
					alert("成功删除节点");
					instance.onRefresh();
					instance.onAdd();
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据批量删除操作
		 **********************************************************************/
		this.onBatDelete = function() {
			var rows = instance.getSelectedRows();
			if (undefined == rows || rows.length == 0) {
				alert("未选中行");
				return;
			}

			if (!confirm("您确定要删除选中记录")) {
				return;
			}
			if ($("#batDeleteForm").length == 0) {
				alert("未配置数据来源表单");
				return;
			}

			var array = new Array();
			for ( var index = 0; index < rows.length; index++) {
				var row = rows[index];
				for ( var x = 0; x < instance.pfields.length; x++) {
					var jobject = new Object();
					jobject[instance.pfields[x]] = row[instance.pfields[x]];
					array[array.length] = jobject;
				}
			}

			$("#batDeleteContents").val($.toJSON(array));
			$('#batDeleteForm').ajaxSubmit(function(data) {
				if ("成功删除记录" == data) {
					alert(data);
					$("#gridTable").datagrid('reload');
				} else {
					alert(data);
				}
			});
		};

		/***********************************************************************
		 * 数据情况操作
		 **********************************************************************/
		this.onClear = function() {

		};

		/***********************************************************************
		 * 页面跳转操作
		 **********************************************************************/
		this.onForward = function() {

		};

		/***********************************************************************
		 * Excel导入操作
		 **********************************************************************/
		this.onExcelImport = function() {

		};

		/***********************************************************************
		 * Excel导出操作
		 **********************************************************************/
		this.onExcelExport = function() {
			
		};

		/***********************************************************************
		 * 视图刷新
		 **********************************************************************/
		this.onRefresh = function() {
			$("#westTree").tree("reload");
			instance.viewOperate.refreshOperates();
		};

		this.onSearch = function() {

		};

		/***********************************************************************
		 * 获取视图多选行
		 **********************************************************************/
		this.getSelectedRows = function() {
			return;
		};

		/***********************************************************************
		 * 获取视图选中行
		 **********************************************************************/
		this.getSelectedRow = function() {
			var node	=	$("#westTree").tree("getSelected");
			return node;
		};
	};

	$.fn.listView = function(action) {
		var i, ilen, args, output, userSettings, $this = this.first(), instance = $this
				.data('ListView');
		if (typeof action !== 'string') {
			userSettings = action || {};
			if (instance) {
//				instance.updateSettings(userSettings);
			} else {
				instance = new TreeView($this, userSettings);
				$this.data('ListView', instance);
				instance.init();
			}
			return $this;
		} else {
			args = [];
			if (arguments.length > 1) {
				for (i = 1, ilen = arguments.length; i < ilen; i++) {
					args.push(arguments[i]);
				}
			}

			if (instance) {
				if (typeof instance[action] !== 'undefined') {
					output = instance[action].apply(instance, args);
				} else {
					throw new Error('ListView do not provide action: ' + action);
				}
			}
			return output;
		}
	}
})(jQuery);

function showDataSelect(value, rec) {
	return "<input type='checkbox' class='dataSelectCheck'/>";
}

function showDataEdit(value, rec) {
	return "<a href='javascript:onEditData()'><font color=red>修改</font></a>";
}
function showDataDelete(value, rec) {
	return "<a href='javascript:onDeleteData()'><font color=red>删除</font></a>";
}
function showDataEditDelete(value, rec) {
	return "<a href='javascript:onEditData()'>修改</a>&nbsp;<a href='javascript:onDeleteData()'>删除</a>";
}
function showDictMean(value, rec) {
	return "";
}

function onEditData() {
	$("body").data("ListView").onEdit();
}

function onDeleteData() {
	$("body").data("ListView").onDelete();
}

loadResource(application + "/resource/jquery/jquery.form.js", "js");
loadResource(application + "/resource/lib/jquery.json-2.4.js", "js");

loadResource(application + "/resource/platform/skins/ace/js/designForm.js",
		"js");
loadResource(application + "/resource/platform/skins/ace/js/viewOperate.js",
		"js");

loadResource(application + "/resource/platform/skins/ace/js/formInput.js", "js");

loadResource(application + "/resource/jquery/jquery.autocomplete.js", "js");

loadResource(application + "/resource/platform/skins/ace/css/tableDesign.css",
"css");
loadResource(application + "/resource/platform/skins/ace/css/bootstrap.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/ace.min.css",
		"css");
loadResource(application + "/resource/platform/skins/ace/css/font-awesome.min.css",
		"css");
function loadResource(filename, filetype) {
	if (filetype == "js") { // if filename is a external JavaScript file
		var sc = "<script src='" + filename + "'></script>";
		document.write(sc);
	} else if (filetype == "css") { // if filename is an external CSS
		var sc = "<link rel='stylesheet' type='text/css' " + "href='"
				+ filename + "' />";
		document.write(sc);
	}
	if (typeof fileref != "undefined")
		document.getElementsByTagName("head")[0].appendChild(fileref)
}